﻿var ParameterVm = function (jsonOfObject) {
    var self = this;

    self.Id = ko.observable();
    self.OriginalJsonOfObject = jsonOfObject;

    self.Type = "ParameterVm";
    self.Name = ko.observable(null).extend({
        trimString: null,
        required: { message: "Name is a required field.", params: true },
        maxLength: { message: "Please enter less than 50 characters.", params: 50 },
        pattern: { message: "Special characters are not allowed.", params: '^[a-zA-Z0-9 ~/-]+$' }
    });

    self.InputType = ko.observable(null);
    self.ParameterType = ko.observable();
    self.Editing = ko.observable(false);

    self.IsNew = ko.computed(function () {
        return self.Id() == null;
    });

    self.Refresh = function (jsonData) {
        var mapping = {
            'InputType': {
                create: function (options) {
                    return new ParameterInputTypeVm(options.data);
                }
            }
        }

        ko.mapping.fromJS(jsonData, mapping, self);
    };
    
    self.Clone = function () {
        var parameter = new ParameterVm(self.OriginalJsonOfObject);
        return parameter;
    };

    self.Refresh(jsonOfObject);

    self.MapToJson = function () {
        var json = {};
        json["Id"] = self.Id();
        json["Name"] = self.Name != null ? self.Name : self.Name();
        json["ParameterType"] = self.ParameterType();
        json["InputType"] = self.InputType().MapToJson();
        return json;
    };
};